<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { cacheAdd, cacheFind, cacheFindById, cacheModify, cacheRemove } from '@/api/system/cache'
import { Message, Modal } from '@arco-design/web-vue'
import { useGlobalStore } from '@/stores/global'
import router from '@/router'

// 搜索表单
const searchForm = reactive({
  // 搜索
  search: () => {
    cacheFind({}, {
      pageNum: searchForm.list.pageNum,
      pageSize: searchForm.list.pageSize
    }).then((resp: any) => {
      searchForm.list = resp.data
    })
  },
  // table 数据
  list: {
    list: [],
    total: 0,
    pageSize: 10,
    pageNum: 1
  }
})

// 操作数据表单
const operationForm = reactive({
  // 弹框标题
  title: '添加用户',
  // 是否显示
  visible: false,
  // 表单
  form: {} as any,
  // 验证
  rules: [
    {}
  ],
  reset: () => {
    operationForm.form = {
      id: null,
      code: '',
      name: ''
    }
  },
  // 打开弹框
  open: () => {
    operationForm.title = '添加缓存'
    operationForm.visible = true
    operationForm.reset()
  },
  modify: (record: any) => {
    operationForm.title = '修改缓存'
    operationForm.form = record
    operationForm.visible = true
    /*
        cacheFindById(record.id).then(resp => {
          operationForm.form = resp.data
        })
    */
  },
  remove: (record: any) => {
    Modal.confirm({
      title: '警告！',
      content: `是否确认删除 [${record.name}] 缓存？`,
      onOk(e) {
        cacheRemove(record.id)
      }
    })
  },
  submit: ({ errors, values }: any) => {
    if (errors) return
    if (values.id) {
      cacheModify(operationForm.form).then(() => {
        operationForm.visible = false
        searchForm.search()
      })
      return
    }

    cacheAdd(values).then(() => {
      operationForm.visible = false
      searchForm.search()
    })
  },
  //添加缓存值
  addCacheValue: (record: any) => {
    router.push({ path: '/cacheAdd', query: { id: record.id,name:record.name } })
  }
})

onMounted(() => {
  searchForm.search()
  document.title = 'Rocc管理系统 缓存管理'
})

</script>

<template>
  <div class="w-full h-full">
    <div class="flex justify-between mr-1">
      <a-space wrap>
        <a-button type="primary" @click="operationForm.open">
          <template #icon>
            <icon-plus />
          </template>
          添加缓存
        </a-button>
      </a-space>
      <a-space>
        <a-tooltip content="刷新列表" @click="searchForm.search">
          <a-button size="small" shape="circle">
            <template #icon>
              <icon-refresh />
            </template>
          </a-button>
        </a-tooltip>
      </a-space>
    </div>
    <a-drawer v-model:visible="operationForm.visible" :title="operationForm.title" :width="500" :footer="false">
      <a-form ref="operationFormRef" :model="operationForm.form" @submit="operationForm.submit">
        <a-form-item label="名称" field="name">
          <a-input v-model="operationForm.form.name" />
        </a-form-item>
        <a-form-item label="代码" field="code">
          <a-input v-model="operationForm.form.code" :disabled="operationForm.form.id" />
        </a-form-item>
        <a-form-item>
          <a-button type="primary" html-type="submit">
            提交
          </a-button>
        </a-form-item>
      </a-form>
    </a-drawer>

    <a-table :data="searchForm.list.list" :pagination="false">
      <template #columns>
        <a-table-column title="ID" data-index="id"></a-table-column>
        <a-table-column title="名称" data-index="name"></a-table-column>
        <a-table-column title="代码" data-index="code"></a-table-column>
        <a-table-column title="操作" width="160">
          <template #cell="{record}">
            <a-space>
              <a-button status="warning" @click="operationForm.modify(record)">
                <template #icon>
                  <icon-edit />
                </template>
                修改
              </a-button>
              <a-button status="success" @click="operationForm.addCacheValue(record)">
                <template #icon>
                  <icon-plus />
                </template>
                添加缓存值
              </a-button>
              <a-button status="danger" @click="operationForm.remove(record)">
                <template #icon>
                  <icon-delete />
                </template>
                删除
              </a-button>
            </a-space>
          </template>
        </a-table-column>
      </template>
    </a-table>
    <div class="mt-4 flex justify-end">
      <a-pagination v-model:current="searchForm.list.pageNum"
                    v-model:page-size="searchForm.list.pageSize"
                    :total="searchForm.list.total"
                    @change="searchForm.search"
                    show-total
                    show-jumper
                    show-page-size
      ></a-pagination>
    </div>
  </div>
</template>

<style scoped>

</style>
